package com.qianniu.napi.common.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;

public class AddressUtils {

    private static final Logger logger = LoggerFactory.getLogger(AddressUtils.class);
//    private static ClassPathResource classPathResource;
    private static DbSearcher searcher;

    static {
        try {
            DbConfig config = new DbConfig();
            ClassPathResource classPathResource = new ClassPathResource("ip2region/ip2region.db");
            InputStream inputStream = classPathResource.getInputStream();
            File file = File.createTempFile("test", ".db");
            try {
                FileUtils.copyInputStreamToFile(inputStream, file);
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
            logger.debug("======file.getPath()=" + file.getPath());
            searcher = new DbSearcher(config, file.getPath());
        } catch (Exception e) {

        }
    }

    private AddressUtils() {
    }

    private static Logger log = LoggerFactory.getLogger(AddressUtils.class);

    public static String getCityInfo(String ip) {
        try {
            int algorithm = DbSearcher.BTREE_ALGORITHM;
//            DbConfig config = new DbConfig();
//            ClassPathResource classPathResource = new ClassPathResource("ip2region/ip2region.db");
//            InputStream inputStream = classPathResource.getInputStream();
//            File file = File.createTempFile("test", ".db");
//            try {
//                FileUtils.copyInputStreamToFile(inputStream, file);
//            } finally {
//                IOUtils.closeQuietly(inputStream);
//            }
//            logger.debug("======file.getPath()=" + file.getPath());
//
//            DbSearcher searcher = new DbSearcher(config, file.getPath());
            Method method = null;
            switch (algorithm) {
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
                default:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
            }
            DataBlock dataBlock = null;
            if (!Util.isIpAddress(ip)) {
                log.error("Error: Invalid ip address");
            }
            dataBlock = (DataBlock) method.invoke(searcher, ip);
            return dataBlock.getRegion();
        } catch (Exception e) {
            log.error("获取地址信息异常：{}", e);
        }
        return "";
    }
}
